package com.devunion.salon.client.app.mvc.view;

import com.devunion.salon.client.Constants;
import com.devunion.salon.client.app.mvc.SalonEvents;
import com.devunion.salon.client.app.mvc.controller.CompanyController;
import com.devunion.salon.client.app.mvc.controller.ProductListController;
import com.devunion.salon.client.app.mvc.controller.VendorController;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.extjs.gxt.ui.client.widget.toolbar.TextToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * @author Timoshenko Alexander 27.02.2009 : 23:44:42
 */
public class AppView extends View {
    private ContentPanel rootPanel;

    private Menu menu;
    private ToolBar toolBar;

    private ContentPanel centerPanel;

    public AppView(Controller controller) {
        super(controller);
    }

    protected void initialize() {
        rootPanel = new ContentPanel(new BorderLayout());
        toolBar = new ToolBar();
        menu = new Menu();
        centerPanel = new ContentPanel(new FitLayout());
    }

    protected void handleEvent(AppEvent<?> event) {
        switch (event.type) {
            case SalonEvents.APP_INIT:
                initRootPanel();
                break;
        }
    }

    protected void initRootPanel() {
        rootPanel.setHeading("Salon");
        rootPanel.setFrame(true);

        initMenu();
        initCenterPanel();

        Viewport viewport = new Viewport();
        viewport.setLayout(new FitLayout());
        viewport.add(rootPanel);
        
        RootPanel.get().add(viewport);
    }

    protected void initMenu() {
        toolBar = new ToolBar();
        TextToolItem administration = new TextToolItem("Administration");

        menu = new Menu();
        MenuItem companyMenuItem = new MenuItem("Company");
        companyMenuItem.addSelectionListener(new SelectionListener<MenuEvent>() {
            public void componentSelected(MenuEvent event) {
                fireEvent(new AppEvent<String>(SalonEvents.COMPONENT_INIT, CompanyController.COMPANY_CONTROLLER_ID));
            }
        });
        menu.add(companyMenuItem);

        MenuItem productMenuItem = new MenuItem("Product");
        productMenuItem.addSelectionListener(new SelectionListener<MenuEvent>() {
            public void componentSelected(MenuEvent event) {
                fireEvent(new AppEvent<String>(SalonEvents.NAVIGATE_PRODUCT_LIST_VIEW));
            }
        });
        menu.add(productMenuItem);

        MenuItem vendorMenuItem = new MenuItem("Vendor");
        vendorMenuItem.addSelectionListener(new SelectionListener<MenuEvent>() {
            public void componentSelected(MenuEvent event) {
                fireEvent(new AppEvent<String>(SalonEvents.COMPONENT_INIT, VendorController.VENDOR_LIST_CONTROLLER_ID));
            }
        });
        menu.add(vendorMenuItem);

        administration.setMenu(menu);
        toolBar.add(administration);
        rootPanel.setTopComponent(toolBar);
    }

    private void initCenterPanel() {
        centerPanel.setScrollMode(Style.Scroll.AUTO);
        centerPanel.setFrame(true);        

        rootPanel.add(centerPanel, new BorderLayoutData(Style.LayoutRegion.CENTER));

        Registry.register(Constants.APP_CENTER_PANEL, centerPanel);
        Registry.register(Constants.APP_ROOT_PANEL, rootPanel);
    }
}
